#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_rcc.h"
#include "stdio.h"
#include "log.h"
#include "userMath.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesPower.h"
#include "DevicesRT8092.h"

#include "DriverVoltage.h"

#include "taskVoltage.h"


/* 电压监控任务句柄 */
TaskHandle_t g_TaskVoltageHand = NULL;


void vTaskVoltage(void *pvParameters)
{
    uint32_t uiNotifiedValue = 0;
    VoltageInfoType *ptypeVoltageInfoHandle = ptypeVoltageInfoGet();
    PowerInfoType *ptypePowerInfoHandle = ptypePowerInfoGet();
    RT8092InfoType *ptypeRT8092Info = ptypeRT8092InfoGet();

    while(1)
    {
        xTaskNotifyWait(0x00000000, 0xFFFFFFFF, &uiNotifiedValue, 10 / portTICK_RATE_MS);

        /* 需要实时更新数据 */
        vVoltageUpdate();

        /* 监控各通道的电压是否超过限制值 */
        if(ptypePowerInfoHandle->com != DISABLE)
        {
            if(ptypePowerInfoHandle->vdd1 != DISABLE)
            {
                if((ptypeVoltageInfoHandle->vdd1.get > ptypeVoltageInfoHandle->vdd1.limitMax) ||
                   (ptypeVoltageInfoHandle->vdd1.get < ptypeVoltageInfoHandle->vdd1.limitMin))
                {
                    vPowerClose(POWER_CHANNEL_VCC1);
                    ptypeVoltageInfoHandle->vdd1.error = ptypeVoltageInfoHandle->vdd1.get;
                }
            }

            if(ptypePowerInfoHandle->vdd2H != DISABLE)
            {
                if((ptypeVoltageInfoHandle->vdd2H.get > ptypeVoltageInfoHandle->vdd2H.limitMax) ||
                   (ptypeVoltageInfoHandle->vdd2H.get < ptypeVoltageInfoHandle->vdd2H.limitMin))
                {
                    vPowerClose(POWER_CHANNEL_VCC2H);
                    ptypeVoltageInfoHandle->vdd2H.error = ptypeVoltageInfoHandle->vdd2H.get;
                }
            }

            if(ptypePowerInfoHandle->vdd2L != DISABLE)
            {
                if((ptypeVoltageInfoHandle->vdd2L.get > ptypeVoltageInfoHandle->vdd2L.limitMax) ||
                   (ptypeVoltageInfoHandle->vdd2L.get < ptypeVoltageInfoHandle->vdd2L.limitMin))
                {
                    vPowerClose(POWER_CHANNEL_VCC2L);
                    ptypeVoltageInfoHandle->vdd2L.error = ptypeVoltageInfoHandle->vdd2L.get;
                }
            }

            if(ptypePowerInfoHandle->vddQ != DISABLE)
            {
                if((ptypeVoltageInfoHandle->vddQ.get > ptypeVoltageInfoHandle->vddQ.limitMax) ||
                   (ptypeVoltageInfoHandle->vddQ.get < ptypeVoltageInfoHandle->vddQ.limitMin))
                {
                    vPowerClose(POWER_CHANNEL_VCCQ);
                    ptypeVoltageInfoHandle->vddQ.error = ptypeVoltageInfoHandle->vddQ.get;
                }
            }
        }

        /* 微调电压值 */
        if(ptypePowerInfoHandle->com != DISABLE)
        {
            if(ptypePowerInfoHandle->vdd1 != DISABLE)
            {
                /* 误差大于分辨率，需要微调 */
                if(USER_TWO_ABS(ptypeVoltageInfoHandle->vdd1.get, ptypeVoltageInfoHandle->vdd1.set) > fRT8092GetErrorValue(ptypeRT8092Info->vdd1.regBank))
                {
                    if(ptypeVoltageInfoHandle->vdd1.get > ptypeVoltageInfoHandle->vdd1.set)
                        ptypeRT8092Info->vdd1.voltageSet -= fRT8092GetErrorValue(ptypeRT8092Info->vdd1.regBank) * 0.5f;
                    else
                        ptypeRT8092Info->vdd1.voltageSet += fRT8092GetErrorValue(ptypeRT8092Info->vdd1.regBank) * 0.5f;

                    cRT8092ValueSet(RT8092_CHANNEL_VDD1, ptypeRT8092Info->vdd1.voltageSet);
                }
            }

            if(ptypePowerInfoHandle->vdd2H != DISABLE)
            {
                /* 误差大于分辨率，需要微调 */
                if(USER_TWO_ABS(ptypeVoltageInfoHandle->vdd2H.get, ptypeVoltageInfoHandle->vdd2H.set) > fRT8092GetErrorValue(ptypeRT8092Info->vdd2H.regBank))
                {
                    if(ptypeVoltageInfoHandle->vdd2H.get > ptypeVoltageInfoHandle->vdd2H.set)
                        ptypeRT8092Info->vdd2H.voltageSet -= fRT8092GetErrorValue(ptypeRT8092Info->vdd2H.regBank) * 0.5f;
                    else
                        ptypeRT8092Info->vdd2H.voltageSet += fRT8092GetErrorValue(ptypeRT8092Info->vdd2H.regBank) * 0.5f;

                    cRT8092ValueSet(RT8092_CHANNEL_VDD2H, ptypeRT8092Info->vdd2H.voltageSet);
                }
            }

            if(ptypePowerInfoHandle->vdd2L != DISABLE)
            {
                /* 误差大于分辨率，需要微调 */
                if(USER_TWO_ABS(ptypeVoltageInfoHandle->vdd2L.get, ptypeVoltageInfoHandle->vdd2L.set) > fRT8092GetErrorValue(ptypeRT8092Info->vdd2L.regBank))
                {
                    if(ptypeVoltageInfoHandle->vdd2L.get > ptypeVoltageInfoHandle->vdd2L.set)
                        ptypeRT8092Info->vdd2L.voltageSet -= fRT8092GetErrorValue(ptypeRT8092Info->vdd2L.regBank) * 0.5f;
                    else
                        ptypeRT8092Info->vdd2L.voltageSet += fRT8092GetErrorValue(ptypeRT8092Info->vdd2L.regBank) * 0.5f;

                    cRT8092ValueSet(RT8092_CHANNEL_VDD2L, ptypeRT8092Info->vdd2L.voltageSet);
                }
            }

            if(ptypePowerInfoHandle->vddQ != DISABLE)
            {
                /* 误差大于分辨率，需要微调 */
                if(USER_TWO_ABS(ptypeVoltageInfoHandle->vddQ.get, ptypeVoltageInfoHandle->vddQ.set) > fRT8092GetErrorValue(ptypeRT8092Info->vddQ.regBank))
                {
                    if(ptypeVoltageInfoHandle->vddQ.get > ptypeVoltageInfoHandle->vddQ.set)
                        ptypeRT8092Info->vddQ.voltageSet -= fRT8092GetErrorValue(ptypeRT8092Info->vddQ.regBank) * 0.5f;
                    else
                        ptypeRT8092Info->vddQ.voltageSet += fRT8092GetErrorValue(ptypeRT8092Info->vddQ.regBank) * 0.5f;

                    cRT8092ValueSet(RT8092_CHANNEL_VDDQ, ptypeRT8092Info->vddQ.voltageSet);
                }
            }
        }
    }
}
